package drawmodel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;


import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import javax.swing.*;

import com.jogamp.opengl.util.FPSAnimator;

public class ViewFrame extends JFrame implements KeyListener,ActionListener{
	
	// version ID for serialisation
	private static final long serialVersionUID = 1L;
	
	// The JOGL object that handles OpenGL context
	//private GLJPanel jpanel;
	private GLCanvas canvas;
	
	//object to render the scene
	private ViewRender render;
	
	//animator object
	private FPSAnimator animator;
	
	 // Menu items used in the program
	private JMenuItem exitItem;
    private JMenuItem loadFile;
    
    // light items
    private JMenuItem enLight;
    private JMenuItem disLight;
    
    // 10X10 grid menu
    private JMenuItem enGrid;
    private JMenuItem disGrid;
    
    //extra menus
    private JMenuItem aboutItem;
    private JMenuItem helpItem;
    
   //file chooser object
    private JFileChooser fc;
	 
	 public ViewFrame(String title, GLProfile profile){
		super(title);
		
		 fc = new JFileChooser();
		
		// Create the OpenGL Capabilities object
		GLCapabilities caps=new GLCapabilities(profile);
		caps.setDoubleBuffered(true);
			
		canvas =new GLCanvas(caps);
		
		   // Create a new frame
		render=new ViewRender("test_2.tri",60);
		// add the listener object to canvas
		canvas.addGLEventListener(render);
		
		//create the animator 60 frame per second
	    animator =new FPSAnimator(canvas,60);
		
		 // Setup the frame with some default initial parameters
        setSize(600,600);
        setLocationRelativeTo(null);
        
        // exit when the close button is pressed
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.getContentPane().add(canvas);
		canvas.requestFocusInWindow();
		
		//register the key events
		this.addKeyListener(this);
		// build the top menu
		buildMenu();
		
        setVisible(true);
        requestFocusInWindow();
        
        //start the animation
        animator.start();
	
	}
	
	
	public static void main(String[] args){
		  // This line initialises some OpenGL features 
		GLProfile.initSingleton(true);
		// Get the default profile
		GLProfile profile=GLProfile.getDefault();
		
		// make a new frame
		new ViewFrame("Graphics Coursework", profile);
		
	}

	
	
	@Override
	public void keyPressed(KeyEvent key) {
		switch(key.getKeyCode()){
		// move the camera to top down
		case KeyEvent.VK_UP : 
			render.camPositionChange(0, 1,0);
			break;
		case KeyEvent.VK_DOWN: 
			render.camPositionChange(0, -1, 0);
			break;
		
		// move the camera to left right
	    case KeyEvent.VK_LEFT: 
	    	render.camPositionChange(-1, 0,0); 
	    	break;
		case KeyEvent.VK_RIGHT: 
			render.camPositionChange(1, 0, 0); 
			break;
	    // zoom the camera
		case KeyEvent.VK_U: render.camPositionChange(0, 0, -1); break;
		case KeyEvent.VK_J: render.camPositionChange(0, 0, 1); break;
		
		//set up ambient light
		case KeyEvent.VK_A: render.setAmbLight(0.1f, 0, 0); break;
		case KeyEvent.VK_Q: render.setAmbLight(-0.1f, 0, 0); break;
		
		case KeyEvent.VK_S: render.setAmbLight(0, 0.1f, 0); break;
		case KeyEvent.VK_W: render.setAmbLight(0, -0.1f, 0); break;
		case KeyEvent.VK_D: render.setAmbLight(0, 0, 0.1f); break;
		case KeyEvent.VK_E: render.setAmbLight(0, 0, -0.1f); break;
		
		//set up light positions
		case KeyEvent.VK_Z: render.setLightPos(0.5f,0,0); break;
		case KeyEvent.VK_X: render.setLightPos(0,0.5f,0); break;
		case KeyEvent.VK_C: render.setLightPos(0,0,0.5f); break;
		
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * Construct the main menu we are going to use in the program
	 */
	private void buildMenu() {
        // Add the menu bar
        JMenuBar menuBar = new JMenuBar();
        
        // Build the file menu
        JMenu fileMenu = new JMenu("File");
        
        // load menu to load the file
        loadFile = new JMenuItem("LoadModel");
        loadFile.addActionListener(this);
        fileMenu.add(loadFile);
        menuBar.add(fileMenu);
        
        // Add an item to exit the program
        exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(this);
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);
        
       
        
        //Build the Lightning menu
        JMenu lightMenu = new JMenu("Lighting");
        // Add an item to enable lightning
        enLight = new JMenuItem("Enable Light");
        enLight.addActionListener(this);
        lightMenu.add(enLight);
        
        // Add an item to disable lightning
        disLight = new JMenuItem("Disable Light");
        disLight.addActionListener(this);
        lightMenu.add(disLight);
        menuBar.add(lightMenu);
        
        // Build the Grid menu
        JMenu gridmenu = new JMenu("Grid    ");
        // Add an item to enable the grid
        enGrid = new JMenuItem("Enable Grid");
        enGrid.addActionListener(this);
        gridmenu.add(enGrid);
        
        // Add an item to disable the grid
        disGrid = new JMenuItem("Disable Grid");
        disGrid.addActionListener(this);
        gridmenu.add(disGrid);
        
        menuBar.add(gridmenu);
        
        // Build the Help menu
        JMenu helpMenu = new JMenu("Help");

        helpItem = new JMenuItem("Help");
        helpItem.addActionListener(this);
        helpMenu.add(helpItem);
        

        aboutItem = new JMenuItem("About");
        aboutItem.addActionListener(this);
        helpMenu.add(aboutItem);
        menuBar.add(helpMenu);
        
        // Add the menu bar to this Frame
        setJMenuBar(menuBar);
	    
	}

	
	/*
	 * to stop the animation
	 */
		
	public void stopAnimate(){
		animator.stop();
	}

/*
 * mouse action
 * (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
	@Override
	public void actionPerformed(ActionEvent e) {
		   // Determine which menu item is selected and act appropriately
		if (e.getSource().equals(exitItem))
		{
			  System.exit(0);
			
		}
		if (e.getSource().equals(loadFile)){
			// load the file chooser menu to select the .tri files
			 int returnVal =fc.showOpenDialog(this);
			  if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                String s= file.getAbsolutePath();
	                
	                // set the reference to new Render Object after load the file
	                render=new ViewRender(s,60);
	    			canvas.addGLEventListener(render);
	                
			  }
		}
		
        if (e.getSource().equals(enLight))
        {
          // set light enable variable to true
          render.setEnableLight(true);
        }
        if (e.getSource().equals(disLight))
        {
          // set light enable variable to false to disable the lightning
          render.setEnableLight(false);
        }
        if (e.getSource().equals(enGrid))
        {
        // set grid enable variable to true
            render.setEnGrid(true);
        }
        if (e.getSource().equals(disGrid))
        {
        	// set grid enable variable to false
            render.setEnGrid(false);
        }
        
        if (e.getSource().equals(aboutItem))
        {
             displayAboutMessage();
        }
        if (e.getSource().equals(helpItem))
        {
            
            displayHelpMessage();
        }
		
	}
	
	 private void displayAboutMessage(){
		 JOptionPane.showMessageDialog(this,
	                "The Assignment by A.Karesh",
	                "OpenGL",
	                JOptionPane.INFORMATION_MESSAGE);
	    
		 
	 }
	
	 private void displayHelpMessage(){
		 JOptionPane.showMessageDialog(this,
	                "Camera Moves: UP DOWN LEFT RIGHT \n " +
	                "Camera Zoom: U Zoom in and J zoom out \n " +
	                "Ambient Light: A Increase Red Q decrease Red  \n" +
	                "Ambient Light: S Increase Green W decrease Green  \n"+
	                "Ambient Light: D Increase Blue E decrease Blue \n" +
	                "Light Position: Increase X   press Z  \n" +
	                "Light Position: Increase Y   press X \n" +
	                "Light Position: Increase Z   press C \n",
	                "OpenGL",
	                JOptionPane.INFORMATION_MESSAGE);
	    
		 
	 }
	
}
